﻿<%@ WebHandler Language="C#" Class="upload_image" %>

using System;
using System.Collections;
using System.Web;
using LitJson;

public class upload_image : IHttpHandler {

    public void ProcessRequest(HttpContext context)
    {
        HttpPostedFile file = context.Request.Files["imgFile"];
        string uploaddir = "../../upload/images/";
        uploaddir += DateTime.Now.ToString("yyyy/MM/dd/");
        string uploadpath = HttpContext.Current.Server.MapPath(uploaddir);
        string filename = DateTime.Now.ToString("HHmmssffff-") + file.FileName;
        if (file != null)
        {
            Cstudy.Tools.DirectoryHelp.Create(uploadpath);
            file.SaveAs(uploadpath + filename);
            string jsonstr = "{\"error\":0,\"url\":" + uploaddir + filename + "}";
            System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
            Hashtable json = new Hashtable();
            json["error"] = 0;
            json["url"] = uploaddir.Replace("../..", "") + filename;
            context.Response.AddHeader("Content-Type", "text/html; charset=UTF-8");
            string result = JsonMapper.ToJson(json);
            context.Response.Write(result);
            context.Response.End();
        }
        else
        {
            Hashtable json = new Hashtable();
            json["error"] = 1;
            json["message"] = "图片为空";
            context.Response.AddHeader("Content-Type", "text/html; charset=UTF-8");
            context.Response.Write(JsonMapper.ToJson(json));
            context.Response.End();
        }
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}